using System;
using System.Collections.Generic;
using System.Linq;

namespace CF_DAL_WS.Repositories
{
	#region using
    using DIS_DAL_WS.Models;
    using DIS_DAL_WS.Helpers;
	#endregion
	public class CustomerRepository
	{
		public List<Customer> SelectAll()
		{
			try
			{
				return DataContext.Instance.Customer.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Customer>();
		}

		public Customer SelectByID(int id)
		{
			try
			{
				return DataContext.Instance.Customer.FirstOrDefault(item => item.ID == id );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public bool Insert(Customer obj)
		{
			try
			{
				DataContext.Instance.Customer.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.Customer.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.Customer.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public List<Customer> RetrieveByID(int id)
		{
			try
			{
				return (from item in DataContext.Instance.Customer where  item.ID == id  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Customer>();
		}

        public Customer SelectByUserName(string username)
        {
            try
            {
                return DataContext.Instance.Customer.FirstOrDefault(item => item.UserName.Trim().ToUpper() == username.Trim().ToUpper());
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }

        public Customer SelectByUserNameAndPassword(string username, string password)
        {
            try
            {
                return DataContext.Instance.Customer.FirstOrDefault(item => item.UserName.Trim().ToUpper() == username.Trim().ToUpper() && item.Password.Trim().ToUpper() == password.Trim().ToUpper());
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }

	}
}